//
//  OERegisterSecViewController.m
//  OneEightyNine
//
//  Created by apple on 2019/3/3.
//  Copyright © 2019 stoneobs. All rights reserved.
//

#import "OEFindPwdSecendViewController.h"
#import "OEFindPwdDownViewController.h"
@interface OEFindPwdSecendViewController ()<STSendButtonDlegate>
@property(nonatomic, strong) UITextField                     *codeTextFiled;/**<  */
@property(nonatomic, strong) STSendButton                     *sendButton;/**<  */
@property(nonatomic, strong) NSMutableArray                     *titleArray;/**<  */
@property(nonatomic, strong) STButton                     *nextButton;/**<  */
@end

@implementation OEFindPwdSecendViewController
#define CODE_COUNT 6
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configSubView];
    [self addregNotifacations];
    [self st_addAdjustView:self.nextButton offset:10];
    [OERegisterDataController sendGetFindPwdCodeRequestWithMobile:self.phone handle:^(BOOL success, NSString *errorString, NSDictionary *resp) {
        if (success) {
            [self.sendButton st_timerBegin];
        }
    }];
    // Do any additional setup after loading the view.
}
#pragma mark --Notifacation
- (void)addregNotifacations{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(st_reg_textFiledDidChange:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:nil];
    
}
- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)st_reg_textFiledDidChange:(NSNotification*)notify{
    if (notify.object == self.codeTextFiled) {
        [self.codeTextFiled st_textInputLimitWithLength:CODE_COUNT];
        // 配置 验证码
        for (UILabel * lable in self.titleArray) {
            lable.text = @"";
        }
        for (NSInteger i = 0; i < self.codeTextFiled.text.length; i ++) {
            NSString * code = [self.codeTextFiled.text substringWithRange:NSMakeRange(i, 1)];
            STLabel * lable = self.titleArray[i];
            lable.text = code;
        }
        
        if (self.codeTextFiled.text.length == CODE_COUNT) {
            self.nextButton.backgroundColor =OE_LIMITE_COLOR;
            self.nextButton.userInteractionEnabled = YES;
        }else{
            self.nextButton.backgroundColor =UIColor.grayColor;
            self.nextButton.userInteractionEnabled = NO;
        }
    }
}
#pragma mark --configSubView
- (void)configSubView{
    
    self.tableView.backgroundColor = UIColor.whiteColor;
    NSString * phone = [NSString stringWithFormat:@"%@ %@",OE_PHONE_PREFIX,self.phone.st_safetyPhone];
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    
    STLabel * changeLabel = [[STLabel alloc] initWithFrame:CGRectMake(22, 20, 200, 40)
                                                      text:OESTRING(@"it has been sent to")
                                                 textColor:SecendTextColor
                                                      font:15
                                               isSizetoFit:NO
                                             textAlignment:NSTextAlignmentLeft];
    [footer addSubview:changeLabel];
    
    STLabel * alertLabel = [[STLabel alloc] initWithFrame:CGRectMake(22, changeLabel.bottom, UIScreenWidth, 30)
                                                     text:phone
                                                textColor:FirstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    //    alertLabel.attributedText = [alertLabel.text st_convertAttributeStringWithKeyWord:phone attributes:@{NSForegroundColorAttributeName:TM_YellowTitleGroundColor}];
    [footer addSubview:alertLabel];
    
    
    
    self.sendButton = [[STSendButton alloc] initWithFrame:CGRectMake(0, 0, 74, 24) andWithDuration:60];
    [self.sendButton st_setBorderWith:0.5 borderColor:TM_YellowBackGroundColor cornerRadius:2];
    self.sendButton.layer.cornerRadius = 2;
    self.sendButton.clipsToBounds = YES;
    self.sendButton.delegate = self;
    [self.sendButton setTitleColor:SecendTextColor forState:UIControlStateNormal];
    [self.sendButton setTitleColor:SecendTextColor forState:UIControlStateSelected];
    [self.sendButton setTitle:@"CODE" forState:UIControlStateNormal];
    self.sendButton.titleLabel.font = [UIFont systemFontOfSize:10];
    self.sendButton.backgroundColor = UIColor.whiteColor;
    [self.sendButton st_setBorderWith:0.5 borderColor:TM_lineColor cornerRadius:12];
    self.sendButton.right = UIScreenWidth - 22;
    self.sendButton.centerY = changeLabel.centerY;
    [footer addSubview:self.sendButton];
    
    
    STLabel * seLabel = [[STLabel alloc] initWithFrame:CGRectMake(22, alertLabel.bottom+40, UIScreenWidth, 40)
                                                  text:OESTRING(@"SECURITY CODE")
                                             textColor:FirstTextColor
                                                  font:14
                                           isSizetoFit:NO
                                         textAlignment:NSTextAlignmentLeft];
    //    alertLabel.attributedText = [alertLabel.text st_convertAttributeStringWithKeyWord:phone attributes:@{NSForegroundColorAttributeName:TM_YellowTitleGroundColor}];
    [footer addSubview:seLabel];
    
    self.codeTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(22, seLabel.bottom, footer.width - 44, 50)];
    self.codeTextFiled.keyboardType = UIKeyboardTypeNumberPad;
    self.codeTextFiled.textColor = UIColor.clearColor;
    self.codeTextFiled.tintColor = [UIColor clearColor];
    [footer addSubview:self.codeTextFiled];
    
    //配置 label
    self.titleArray = NSMutableArray.new;
    CGFloat max = (self.codeTextFiled.width - 74);
    CGFloat lableWith = (max - CODE_COUNT*5 - 30)/CODE_COUNT;
    for (NSInteger i = 0; i < CODE_COUNT; i ++) {
        STLabel * titleLabel = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, lableWith, 48)
                                                         text:@""
                                                    textColor:FirstTextColor
                                                         font:15
                                                  isSizetoFit:NO
                                                textAlignment:NSTextAlignmentCenter];
        [self.codeTextFiled addSubview:titleLabel];
        [self.titleArray addObject:titleLabel];
        titleLabel.userInteractionEnabled = NO;
        UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, titleLabel.bottom, titleLabel.width, 0.5)];
        line.backgroundColor = OE_LIMITE_COLOR;
        [self.codeTextFiled addSubview:line];
        titleLabel.left = i * lableWith + 5 * i + 0;
        line.centerX = titleLabel.centerX;
        titleLabel.userInteractionEnabled = NO;
    }
    
    __weak typeof(self) weakSelf =  self;
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(5, self.codeTextFiled.bottom + 80, UIScreenWidth - 30, 44)
                                                        title:OESTRING(@"NEXT")
                                                   titleColor:UIColor.whiteColor
                                                    titleFont:15
                                                 cornerRadius:22
                                              backgroundColor:UIColor.grayColor
                                              backgroundImage:nil
                                                        image:nil];
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    confimButton.centerX = self.tableView.width / 2;
    [footer addSubview:confimButton];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedNextButton];
    }];
    self.nextButton = confimButton;
    self.nextButton.userInteractionEnabled = NO;
    
    footer.height = self.nextButton.bottom;
    self.tableView.tableFooterView = footer;
}
#pragma mark --STSendButtonDlegate
- (BOOL)st_sendButtonWillClic:(UIButton*)button{
    return YES;
}
- (void)st_sendButtonDidClic:(UIButton*)button isFirstClic:(BOOL)isFirstClic duration:(NSInteger)duration{
    if (isFirstClic) {
        [OERegisterDataController sendGetFindPwdCodeRequestWithMobile:self.phone handle:^(BOOL success, NSString *errorString, NSDictionary *resp) {
            if (success) {
                [self.sendButton st_timerBegin];
            }else{
                [self.sendButton st_timerEnd];
            }
        }];
    }
}

- (void)st_sendButtonDidCountdown:(UIButton*)button duration:(NSInteger)duration{
    [self.sendButton setTitle:@(duration).description forState:UIControlStateNormal];
}
- (void)st_sendButtonTimeEnded:(UIButton*)button{
    [self.sendButton st_timerEnd];
    [self.sendButton setTitle:@"重新发送" forState:UIControlStateNormal];
}

#pragma mark --Action Method
- (void)onSelctedNextButton{
    __weak typeof(self) weakSelf =  self;
    [OERegisterDataController sendJudgeFindPwdCodeCrrectRequestWithMobile:self.phone mobile_code:self.codeTextFiled.text handle:^(BOOL success, NSString *errorString, NSDictionary *resp) {
        if (success) {
            OEFindPwdDownViewController * vc = OEFindPwdDownViewController.new;
            vc.phone = weakSelf.phone;
            vc.code = weakSelf.codeTextFiled.text;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }
    }];
    
}@end
